package com.stox.ui.javafx.charting.model.plot;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.stox.dto.AbstractModel;
import com.stox.dto.AbstractBar;
import com.stox.ui.javafx.charting.model.uiunit.BarPrimaryUiUnit;
import com.stox.ui.javafx.charting.strategy.DefaultBarPrimaryUiUnitColorStrategy;
import com.stox.ui.javafx.charting.widget.PrimaryChart;

@Component("barPrimaryPlot")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class BarPrimaryPlot extends AbstractPrimaryPlot<	AbstractBar, 
																				PrimaryChart<BarPrimaryPlot>, 
																				BarPrimaryUiUnit, 
																				BarPrimaryPlot> {
	
	private ApplicationContext applicationContext;
	
	public BarPrimaryPlot(AbstractModel model) {
		super(model);
	}
	
	@Autowired
	public void setApplicationContext(ApplicationContext applicationContext){
		this.applicationContext = applicationContext;
		setColorStrategy(applicationContext.getBean(DefaultBarPrimaryUiUnitColorStrategy.class));
	}
	
	public void setVolumeData() {
		if(null == getChart() || null == getChart().getChartingView()) return;
		VolumePlot volumePlot = getChart().getChartingView().getVolumePlot(); 

		if(null == volumePlot || !volumePlot.getModel().equals(getChart().getFrontPlot().getModel())) {
			if(null != volumePlot) volumePlot.removeFromChart();
			volumePlot = (VolumePlot)applicationContext.getBean("volumePlot", getChart().getChartingView(), getChart().getFrontPlot().getModel());
			getChart().getChartingView().setVolumePlot(volumePlot);
		}
		
		List<Number> data = new ArrayList<>(getData().size());
		for(AbstractBar bar : getData()) data.add(bar.getVolume());
		volumePlot.setData(data);
	}
	
	@Override
	protected void createUiUnits() {
		for(int i = 0; i < getData().size(); i++)			
			getUiUnits().add(new BarPrimaryUiUnit(getData().get(i),this,i));
	}
	
	@Override
	public void update() {
		super.update();
		
		for(int index = getChart().getChartingView().getEndIndex()-1; 
				index >= getChart().getChartingView().getStartIndex(); index--) {
			
			AbstractBar bar = getData().get(index);
			if (highest < bar.getHigh())
				highest = bar.getHigh();
			if (lowest > bar.getLow())
				lowest = bar.getLow();
			volumeSum += bar.getVolume();
		}
		
		for(int index = getChart().getChartingView().getEndIndex()-1; 
				index >= getChart().getChartingView().getStartIndex(); index--) {
			getUiUnits().get(index).update();
		}
	}
	
	@Override
	public void paint() {
		for(int index = getChart().getChartingView().getEndIndex()-1; 
				index >= getChart().getChartingView().getStartIndex(); index--) {
			getUiUnits().get(index).paint();
		}
	}

}
